package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"wx-api/ssp-api/handlers"
	"wx-api/ssp-api/middleware"
	"wx-api/ssp-api/services/se_report"
	se_sspslot "wx-api/ssp-api/services/se_slot"
	"wx-api/ssp-api/services/se_sspapp"
	"wx-api/ssp-api/services/se_template"
	"wx-api/ssp-api/services/se_user"
)

func RouteApi(m string) *gin.Engine {
	var r *gin.Engine
	//if m == "product" {
	//	r = gin.New()
	//} else {
	//	gin.SetMode("debug")
	//	r = gin.Default()
	//}
	gin.SetMode("debug")
	r = gin.Default()
	r.Use(cors())

	//负载探活
	r.Any("/", Rz)
	Api := r.Group("/api/v1/")
	//登录
	Api.POST("login", func(c *gin.Context) {
		se_user.Login(handlers.NewBaseController(c))
	})
	Api.POST("login_out", func(c *gin.Context) {
		se_user.LoginOut(handlers.NewBaseController(c))
	})
	app_api := Api.Group("/")
	app_api.Use(middleware.CheckSspLogin())
	//获取账户详情
	app_api.GET("uinfo", func(c *gin.Context) {
		se_user.GetUserInfo(handlers.NewBaseController(c))
	})
	//应用相关
	ssp_app := app_api.Group("/app")
	ssp_app.GET("/list", func(c *gin.Context) {
		se_sspapp.List(handlers.NewBaseController(c))
	})
	ssp_app.POST("/add", func(c *gin.Context) {
		se_sspapp.Add(handlers.NewBaseController(c))
	})
	ssp_app.POST("/edit", func(c *gin.Context) {
		se_sspapp.Edit(handlers.NewBaseController(c))
	})
	ssp_app.GET("/info", func(c *gin.Context) {
		se_sspapp.Info(handlers.NewBaseController(c))
	})
	ssp_app.GET("/flag", func(c *gin.Context) {
		se_sspapp.Flag(handlers.NewBaseController(c))
	})
	ssp_app.GET("/stats", func(c *gin.Context) {
		se_sspapp.Stats(handlers.NewBaseController(c))
	})
	ssp_app.GET("/appall", func(c *gin.Context) {
		se_sspapp.All(handlers.NewBaseController(c))
	})
	ssp_app.GET("/tempall", func(c *gin.Context) {
		se_template.TempAll(handlers.NewBaseController(c))
	})
	//广告位相关
	ssp_slot := app_api.Group("/slot")
	ssp_slot.GET("/list", func(c *gin.Context) {
		se_sspslot.List(handlers.NewBaseController(c))
	})
	ssp_slot.POST("/add", func(c *gin.Context) {
		se_sspslot.Add(handlers.NewBaseController(c))
	})
	ssp_slot.POST("/edit", func(c *gin.Context) {
		se_sspslot.Edit(handlers.NewBaseController(c))
	})
	ssp_slot.GET("/info", func(c *gin.Context) {
		se_sspslot.Info(handlers.NewBaseController(c))
	})
	ssp_slot.GET("/flag", func(c *gin.Context) {
		se_sspslot.Flag(handlers.NewBaseController(c))
	})
	ssp_slot.GET("/stats", func(c *gin.Context) {
		se_sspslot.Stats(handlers.NewBaseController(c))
	})
	//报表相关
	ssp_report := app_api.Group("/report")
	ssp_report.POST("/list", func(c *gin.Context) {
		se_report.SspReport(handlers.NewBaseController(c))
	})
	ssp_report.GET("/index", func(c *gin.Context) {
		se_report.SspReportIndex(handlers.NewBaseController(c))
	})
	return r
}

func cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		origin := c.Request.Header.Get("Origin")
		c.Header("Access-Control-Allow-Origin", origin) // 这是允许访问所有域
		c.Header("Access-Control-Allow-Methods", "*")   //服务器支持的所有跨域请求的方法,为了避免浏览次请求的多次'预检'请求
		//header的类型
		c.Header("Access-Control-Request-Headers", "*")
		c.Header("Access-Control-Allow-Headers", "*")
		//允许跨域设置                                                                                                      可以返回其他子段
		c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Content-Type,token") // 跨域关键设置 让浏览器可以解析
		//c.Header("Access-Control-Max-Age", "172800")        // 缓存请求信息 单位为秒
		c.Header("Access-Control-Allow-Credentials", "true")
		//c.Set("content-type", "application/json") // 设置返回格式是json
		//fmt.Println(c.Request.RequestURI)
		//fmt.Println(c.Request.Method)

		//拦截所有options请求
		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(200)
			return
		}

		c.Next()
		return

	}
}

func Rz(c *gin.Context) {
	str := make(map[string]string)
	str["msg"] = "success"
	c.JSON(http.StatusOK, str)
	return
}
